package com.spring.framework.aop.annotation.advice;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 启动类
 *
 * @author xuweizhi
 * @since 2020/12/16 15:08
 */
@Configuration
@SuppressWarnings("all")
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ComponentScan(basePackages = "com.spring.framework.aop.annotation.advice")
public class PointConfig {

	public static void main(String[] args) {
		//1 获取容器
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PointConfig.class);
		//2.获取对象
		Child child = (Child) ac.getBean("child");
		Parent parent = (Parent) ac.getBean("parent");
		//3.尝试调用被代理类的相关方法
		System.out.println("---------");
		//子类bean调用父类的方法
		child.target1();
		//子类bean调用重写的方法
		child.target2();
		//子类bean调用自己的方法
		child.target3();

		System.out.println("=======");
		//具有该注解的类调用自己的方法，会被匹配
		parent.target1();
	}

	@Test
	public void aspectAdvice() {
		//1 获取容器
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PointConfig.class);
		//2 获取目标类bean
		AspectAdviceTarget aspectAdviceTarget = ac.getBean("aspectAdviceTarget", AspectAdviceTarget.class);
		//2 调用目标方法
		aspectAdviceTarget.target();
	}


}
